package com.hjm.common.utils;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName RequestUtils
 * @Deacription 请求处理类
 * @Author Gd
 * @Date 2022/2/10 0010 10:34
 * @Version 1.0
 **/
public class RequestUtils {

    /**
     * 获取请求当中的文件
     * @Param [request]
     * @Return org.springframework.web.multipart.MultipartFile
     */
    public static List<MultipartFile> getMultipartFile(HttpServletRequest request){
        List<MultipartFile> multipartList = new ArrayList<>();
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            Iterator iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                MultipartFile img = multiRequest.getFile(iter.next().toString());
                multipartList.add(img);
            }
        }
        return multipartList;
    }
}
